/*****************************************************************
 JADE - Java Agent DEvelopment Framework is a framework to develop 
 multi-agent systems in compliance with the FIPA specifications.
 Copyright (C) 2000 CSELT S.p.A. 
 
 GNU Lesser General Public License
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation, 
 version 2.1 of the License. 
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the
 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA  02111-1307, USA.
 *****************************************************************/

package jade.oo.core.behaviours;

import jade.oo.core.behaviours.state.BehaviourFinished;
import jade.oo.core.behaviours.state.BehaviourState;
import jade.oo.core.behaviours.state.BehaviourStillActive;
import jade.oo.core.behaviours.state.BehaviourWaitingForMessage;

public abstract class TickerBehaviour implements Behaviour {
	private long wakeupTime, period;
	private boolean finished;
	private int tickCount = 0;
	private jade.oo.core.behaviours.TickerAction delegate;

	public TickerBehaviour(long period,
			jade.oo.core.behaviours.TickerAction tickerAction) {
		if (period <= 0) {
			throw new IllegalArgumentException("Period must be greater than 0");
		}
		this.period = period;
		this.delegate = tickerAction;
	}

	@Override
	public void onStart() {
		wakeupTime = System.currentTimeMillis() + period;
	}

	@Override
	public final BehaviourState action() {
		// Someone else may have stopped us in the meanwhile
		if (!finished) {
			long blockTime = wakeupTime - System.currentTimeMillis();
			if (blockTime <= 0) {
				// Timeout is expired --> execute the user defined action and
				// re-initialize wakeupTime
				tickCount++;
				onTick();
				wakeupTime = System.currentTimeMillis() + period;
				blockTime = period;
			}
			// Maybe this behaviour has been removed within the onTick() method
			if (!finished) {
				return new BehaviourWaitingForMessage(blockTime);
			}
		}
		return new BehaviourFinished();
	}

	@Override
	public int onEnd() {
		return 0;
	}

	private void onTick() {
		delegate.onTick();
	}

	public final int getTickCount() {
		return tickCount;
	}

	// #MIDP_EXCLUDE_BEGIN

	// For persistence service
	private void setTickCount(int tc) {
		tickCount = tc;
	}

	// For persistence service
	private void setPeriod(long p) {
		period = p;
	}

	// For persistence service
	private long getPeriod() {
		return period;
	}

	// For persistence service
	private void setWakeupTime(long wt) {
		wakeupTime = wt;
	}

	// For persistence service
	private long getWakeupTime() {
		return wakeupTime;
	}

	// #MIDP_EXCLUDE_END

}
